package cn.fbs.service;


import cn.fbs.dao.dm.DMDataInfoMapper;
import cn.fbs.dao.dm.DMImageInfoMapper;
import cn.fbs.dao.dm.DMNewTargetMapper;
import cn.fbs.dao.dm.DMVideoInfoMapper;
import cn.fbs.model.NewTcodeTargetTree;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Service
public class JointTrialService {
    @Autowired
    private DMImageInfoMapper imageInfoMapper;
    @Autowired
    private DMDataInfoMapper dataInfoMapper;
    @Autowired
    private DMVideoInfoMapper videoInfoMapper;
    @Autowired
    private DMNewTargetMapper targetMapper;
    @Autowired
    private NewTargetService newTargetService;
//    public Object getAllResultData(int mediaType,int pageNo,int pageSize) {
//        if (mediaType== MediaEnum.IMAGE.getIndex()){
//            PageHelper.startPage(pageNo,pageSize);
//            List<TImageInfo> list = imageInfoMapper.getAllResultData();
//            return new PageInfo<>(list);
//        }
//
//        if (mediaType== MediaEnum.VIDEO.getIndex()){
//            PageHelper.startPage(pageNo,pageSize);
//            List<TVideoInfo> list = videoInfoMapper.getAllResultData();
//            return new PageInfo<>(list);
//        }
//
//        return null;
//    }

    public List<NewTcodeTargetTree> getMyTarget(List<String> targets) {
        targets = targets.stream().distinct().collect(Collectors.toList());
        String idsStr = StringUtils.join(targets, "','");
        ArrayList<String> list = new ArrayList<>();
        getPid(targets,list);
        String pidStr = StringUtils.join(list, "','");
        /**
         * 避免字符串截断-可以没有儿子，但是要有爹
         */
        System.out.println(" List<NewTcodeTargetTree> targetTrees=targetMapper.getBelongMeTarget(");
//        List<NewTcodeTargetTree> targetTrees=targetMapper.getBelongMeTarget(
//                "'"+idsStr+"'",
//                "("+likeStr+")",
//                "'"+pidStr+"'");
        List<NewTcodeTargetTree> targetTrees = targetMapper.getBelongMeTargetByPid("'" + pidStr + "'");

        List<NewTcodeTargetTree> belongMeTargetByIds = targetMapper.getBelongMeTargetByIds("'" + idsStr + "'");

//        List<NewTcodeTargetTree> belongMeTargetByLike = new ArrayList<>();
//
//        ArrayList<String> ids = null;
//        for (int i = 0; i < targets.size(); i++) {
//            if (i%10==0||i==targets.size()-1){
//                if (i!=0){
//                    String likeStr=StringUtils.join(ids, "|");
//                    List<NewTcodeTargetTree> subData = targetMapper.getBelongMeTargetByLike("'" + likeStr + "'");
//                    belongMeTargetByLike.addAll(subData);
//                }
//                ids=new ArrayList<String>();
//            }
//            ids.add(targets.get(i));
//        }

//             for (NewTcodeTargetTree belongMeTargetById : belongMeTargetByIds) {
//            for (NewTcodeTargetTree targetTree : belongMeTargetByLike) {
//                if (belongMeTargetById.getXh()!=targetTree.getXh()){
//                    targetTrees.add(targetTree);
//                }
//            }
//        }
        targetTrees.addAll(belongMeTargetByIds);
        ArrayList<String> myList = new ArrayList<>();
        for (NewTcodeTargetTree targetTree : targetTrees) {
            if (targetTree.isBelongMe()){
                myList.add(targetTree.getXh());
            }
        }
        List<String> collect = myList.stream().distinct().collect(Collectors.toList());
        Iterator<NewTcodeTargetTree> iterator = targetTrees.iterator();
        while (iterator.hasNext()){
            NewTcodeTargetTree next = iterator.next();
            for (String xh : collect) {
                if (xh.equalsIgnoreCase(next.getXh())&&!next.isBelongMe()){
                    iterator.remove();
                }
            }
        }
        ArrayList<NewTcodeTargetTree> topNodes = newTargetService.formatTree(targetTrees);
        return topNodes;
    }

    private void getPid(List<String> targets,ArrayList<String> list) {
        for (String targetStr : targets) {
            for (int i = 0; i < targetStr.length()/2-1; i++) {
                String substring = targetStr.substring(0, targetStr.length() - (2+i*2));
                list.add(substring);
            }
        }

    }


    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("dd");
        list.add("sd");
        list.add("dk");
        list.add("dd");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(i);
            System.out.println(list.get(i));
        }
//        Iterator<String> iterator = list.iterator();
//        while (iterator.hasNext()){
//            String next = iterator.next();
//            System.out.println(next);
//            if (next.equalsIgnoreCase("dd")){
//                iterator.remove();
//            }
//        }
//        for (String s : list) {
//            System.out.println("\n"+s);
//        }

        TreeSet<String> strings = new TreeSet<>();
//        TImageInfo
    }






}
